home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / MacWT 0.04 / wt / x11input.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-31  |  4.0 KB  |  195 lines  |  [TEXT/MMCC]

  1. /*
  2. **  wt -- a 3d game engine
  3. **
  4. **  Copyright (C) 1994 by Chris Laurel
  5. **  email:  claurel@mr.net
  6. **  snail mail:  Chris Laurel, 5700 W Lake St #208,  St. Louis Park, MN  55416
  7. **
  8. **  This program is free software; you can redistribute it and/or modify
  9. **  it under the terms of the GNU General Public License as published by
  10. **  the Free Software Foundation; either version 2 of the License, or
  11. **  (at your option) any later version.
  12. **
  13. **  This program is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. **  GNU General Public License for more details.
  17. **
  18. **  You should have received a copy of the GNU General Public License
  19. **  along with this program; if not, write to the Free Software
  20. **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. #include <X11/Xlib.h>
  24. #include <X11/Xutil.h>
  25. #include <X11/keysym.h>
  26.  
  27. #include "wt.h"
  28. #include "error.h"
  29. #include "input.h"
  30. #include "x11graphics.h"
  31.  
  32.  
  33. static void add_special(Intent *intent, int special);
  34.  
  35. static Boolean rotating_cw = False;
  36. static Boolean rotating_ccw = False;
  37. static Boolean moving_forward = False;
  38. static Boolean moving_backward = False;
  39. static Boolean running = False;
  40. static Boolean strafing = False;
  41.  
  42.  
  43. void init_input_devices(void)
  44. {
  45.      XSelectInput(display, wtwin,
  46.           KeyPressMask | KeyReleaseMask | ExposureMask |
  47.           EnterWindowMask | LeaveWindowMask);
  48. }
  49.  
  50.  
  51. void end_input_devices(void)
  52. {
  53. }
  54.  
  55.  
  56. Intent *read_input_devices(void)
  57. {
  58.      static Intent intent;
  59.      int count;
  60.      char buffer[20];
  61.      KeySym key;
  62.      XComposeStatus compose;
  63.      XEvent event;
  64.      int n_events;
  65.  
  66.  
  67.      n_events = XEventsQueued(display, QueuedAlready);
  68.      intent.force_x = intent.force_y = intent.force_z = 0.0;
  69.      intent.force_rotate = 0.0;
  70.      intent.n_special = 0;
  71.  
  72.      while (n_events--) {
  73.       XNextEvent(display, &event);
  74.  
  75.       if (event.type == KeyPress) {
  76.            count = XLookupString(&event.xkey, buffer, 20, &key, &compose);
  77.  
  78.            switch (key) {
  79.           case XK_Left:
  80.             rotating_ccw = True;
  81.             break;
  82.  
  83.           case XK_Right:
  84.             rotating_cw = True;
  85.             break;
  86.  
  87.           case XK_Up:
  88.             moving_forward = True;
  89.             break;
  90.  
  91.           case XK_Down:
  92.             moving_backward = True;
  93.             break;
  94.  
  95.           case XK_Shift_L:
  96.           case XK_Shift_R:
  97.             running = True;
  98.             break;
  99.  
  100.           case XK_Meta_R:
  101.           case XK_Meta_L:
  102.           case XK_Alt_R:
  103.           case XK_Alt_L:
  104.           case XK_slash:
  105.             strafing = True;
  106.             break;
  107.  
  108.           case XK_space:
  109.             add_special(&intent, INTENT_JUMP);
  110.             break;
  111.  
  112.           case XK_q:
  113.             add_special(&intent, INTENT_END_GAME);
  114.             break;
  115.  
  116.           default:
  117.             break;
  118.            }
  119.  
  120.       } else if (event.type == KeyRelease) {
  121.  
  122.            count = XLookupString(&event.xkey, buffer, 20, &key, &compose);
  123.  
  124.            switch (key) {
  125.           case XK_Left:
  126.             rotating_ccw = False;
  127.             break;
  128.  
  129.           case XK_Right:
  130.             rotating_cw = False;
  131.             break;
  132.  
  133.           case XK_Up:
  134.             moving_forward = False;
  135.             break;
  136.  
  137.           case XK_Down:
  138.             moving_backward = False;
  139.             break;
  140.  
  141.           case XK_Shift_L:
  142.           case XK_Shift_R:
  143.             running = False;
  144.             break;
  145.  
  146.           case XK_Meta_R:
  147.           case XK_Meta_L:
  148.           case XK_Alt_R:
  149.           case XK_Alt_L:
  150.           case XK_slash:
  151.             strafing = False;
  152.             break;
  153.  
  154.           default:
  155.             break;
  156.            }
  157.  
  158.       }
  159.      }
  160.      
  161.      if (rotating_cw) {
  162.       if (strafing)
  163.            intent.force_y -= 0.5;
  164.       else
  165.            intent.force_rotate -= 0.5;
  166.      }
  167.      if (rotating_ccw) {
  168.       if (strafing) {
  169.            intent.force_y += 0.5;
  170.       } else
  171.            intent.force_rotate += 0.5;
  172.      }
  173.      if (moving_forward)
  174.       intent.force_x += 0.5;
  175.      if (moving_backward)
  176.       intent.force_x -= 0.5;
  177.      if (running) {
  178.       intent.force_x *= 2.0;
  179.       intent.force_y *= 2.0;
  180.       intent.force_z *= 2.0;
  181.       intent.force_rotate *= 2.0;
  182.      }
  183.  
  184.      return &intent;
  185. }
  186.  
  187.  
  188. void add_special(Intent *intent, int special)
  189. {
  190.      if (intent->n_special < MAX_SPECIAL_INTENTIONS) {
  191.       intent->special[intent->n_special] = special;
  192.       intent->n_special++;
  193.      }
  194. }
  195.